﻿using System;
using System.Threading;
using NUnit.Framework;
using Smartkernel;

namespace Smartkernel.NUnitTest.
{
    [Category("Core")]
    [TestFixture]
    public class SmartThreadTest
    {
        [Test]
        public void Test()
        {
            //【01】异步方法调用（涉及方法：AsynchronousMethodInvoke）
            long start = 0;
            long end = 0;
            start = DateTime.Now.Ticks;
            SmartThread.AsynchronousMethodInvoke(delegate { SmartThread.Sleep(TimeSpan.FromSeconds(10)); });
            end = DateTime.Now.Ticks;
            Assert.AreEqual(TimeSpan.FromTicks(end - start).TotalSeconds < 5, true);

            //【02】基于线程池的异步实现（涉及方法：MultithreadingMethodInvoke）
            start = 0;
            end = 0;
            start = DateTime.Now.Ticks;
            SmartThread.MultithreadingMethodInvoke(delegate { SmartThread.Sleep(TimeSpan.FromSeconds(10)); });
            end = DateTime.Now.Ticks;
            double r = TimeSpan.FromMilliseconds(end - start).TotalSeconds;
            Assert.AreEqual(TimeSpan.FromTicks(end - start).TotalSeconds < 5, true);

            //【03】阻塞线程（涉及方法：SynchronizedMethodInvoke）
            start = 0;
            end = 0;
            start = DateTime.Now.Ticks;
            SmartThread.Sleep(TimeSpan.FromSeconds(2));
            end = DateTime.Now.Ticks;
            Assert.AreEqual(TimeSpan.FromMilliseconds(end - start).TotalSeconds >= 2, true);

            //【04】线程安全的方法调用（涉及方法：SynchronizedMethodInvoke）
            SmartThread.SynchronizedMethodInvoke(delegate { });
        }
    }
}
